package org.mrzc.mvc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MVCInterceptor implements HandlerInterceptor
{
    private static Logger mLog = LoggerFactory.getLogger(MVCInterceptor.class);

    @Override
    // 返回值：表示我们是否需要将当前请求终止
    // false,请求被终止
    // true,请求会被继续
    // cHandler表示被拦截的请求的目标对象
    public boolean preHandle(HttpServletRequest cRequest, HttpServletResponse cResponse,
            Object cHandler) throws Exception
    {
        mLog.debug("------------preHandle-----------" + cHandler.getClass().getName());
        return true;
    }

    @Override
    // 通过cModelAndView来改变显示
    public void postHandle(HttpServletRequest cRequest, HttpServletResponse cResponse,
            Object cHandler, ModelAndView cModelAndView) throws Exception
    {
        mLog.debug("------------postHandle------------");

        cModelAndView.addObject("Value", "1234141");
    }

    // 显示后执行的方法,资源销毁
    @Override
    public void afterCompletion(HttpServletRequest cRequest, HttpServletResponse cResponse,
            Object cHandler, Exception cEx) throws Exception
    {
        mLog.debug("----------afterCompletion------------");
    }

}
